package com.zzyk.main.service.impl;

import com.zzyk.main.mapper.TimeRuleMapper;
import com.zzyk.main.model.pojo.TimeRule;
import com.zzyk.main.model.vo.EnterpriseTimeRule;
import com.zzyk.main.model.vo.OrganizationInfo;
import com.zzyk.main.model.vo.TimeRuleVO;
import com.zzyk.main.service.TimeRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class TimeRuleServiceImpl implements TimeRuleService {

    @Resource
    private TimeRuleMapper timeRuleMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return timeRuleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(TimeRule record) {
        return timeRuleMapper.insert(record);
    }

    @Override
    public int insertSelective(TimeRule record) {
        return timeRuleMapper.insertSelective(record);
    }

    @Override
    public TimeRule selectByPrimaryKey(Integer id) {
        return timeRuleMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(TimeRule record) {
        return timeRuleMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(TimeRule record) {
        return timeRuleMapper.updateByPrimaryKey(record);
    }

    @Override
    public void addTimeRule(Integer eId, String[] beginTime, String[] endTime, String[] cycle) {
        for (int i = 0; i < beginTime.length; i++) {
            TimeRule timeRule = new TimeRule();
            timeRule.setStartTime(beginTime[i]);
            timeRule.setEndTime(endTime[i]);
            timeRule.setEnterprise(eId);
            String[] cycles = cycle[i].split("、");
            for (String s : cycles) {
                switch (s) {
                    case "周一":
                        timeRule.setMonday(true);
                        break;
                    case "周二":
                        timeRule.setTuesday(true);
                        break;
                    case "周三":
                        timeRule.setWednesday(true);
                        break;
                    case "周四":
                        timeRule.setThursday(true);
                        break;
                    case "周五":
                        timeRule.setFriday(true);
                        break;
                    case "周六":
                        timeRule.setSaturday(true);
                        break;
                    case "周日":
                        timeRule.setSunday(true);
                        break;
                }

            }
            timeRuleMapper.insertSelective(timeRule);
        }
    }

    public List<TimeRuleVO> findTimeRuleByParam(String param, String[] areaArr) {
        List<TimeRule> timeRules = timeRuleMapper.selectAllByParam(param, areaArr);
        List<TimeRuleVO> timeRuleVOS = new ArrayList<>();
        for (TimeRule timeRule : timeRules) {
            StringBuffer cycle = new StringBuffer();
            TimeRuleVO timeRuleVO = new TimeRuleVO();
            timeRuleVO.setEnterprise(timeRule.getEName());
            timeRuleVO.setId(timeRule.getId());
            timeRuleVO.setStartTime(timeRule.getStartTime());
            timeRuleVO.setEndTime(timeRule.getEndTime());
            if (timeRule.isMonday()) {
                cycle.append("周一、");
            }
            if (timeRule.isTuesday()) {
                cycle.append("周二、");
            }
            if (timeRule.isWednesday()) {
                cycle.append("周三、");
            }
            if (timeRule.isThursday()) {
                cycle.append("周四、");
            }
            if (timeRule.isFriday()) {
                cycle.append("周五、");
            }
            if (timeRule.isSaturday()) {
                cycle.append("周六、");
            }
            if (timeRule.isSunday()) {
                cycle.append("周日、");
            }
            timeRuleVO.setCycle(cycle.toString().substring(0, cycle.toString().length() - 1));
            timeRuleVOS.add(timeRuleVO);
        }
        return timeRuleVOS;
    }

    @Override
    public void deleteByEnterprise(Integer id) {
        timeRuleMapper.deleteByEnterprise(id);
    }

    @Override
    public List<TimeRuleVO> findTimeRuleByEnterprise(Integer eId) {
        List<TimeRule> timeRules = timeRuleMapper.selectAllByEnterprise(eId);
        List<TimeRuleVO> timeRuleVOS = new ArrayList<>();
        for (TimeRule timeRule : timeRules) {
            StringBuffer cycle = new StringBuffer();
            TimeRuleVO timeRuleVO = new TimeRuleVO();
            timeRuleVO.setEnterprise(timeRule.getEName());
            timeRuleVO.setId(timeRule.getId());
            timeRuleVO.setStartTime(timeRule.getStartTime());
            timeRuleVO.setEndTime(timeRule.getEndTime());
            if (timeRule.isMonday()) {
                cycle.append("周一、");
            }
            if (timeRule.isTuesday()) {
                cycle.append("周二、");
            }
            if (timeRule.isWednesday()) {
                cycle.append("周三、");
            }
            if (timeRule.isThursday()) {
                cycle.append("周四、");
            }
            if (timeRule.isFriday()) {
                cycle.append("周五、");
            }
            if (timeRule.isSaturday()) {
                cycle.append("周六、");
            }
            if (timeRule.isSunday()) {
                cycle.append("周日、");
            }
            timeRuleVO.setCycle(cycle.toString().substring(0, cycle.toString().length() - 1));
            timeRuleVOS.add(timeRuleVO);
        }
        return timeRuleVOS;
    }

    @Override
    public List<EnterpriseTimeRule> findTimeRuleByParam(OrganizationInfo organizationInfo) {
        return timeRuleMapper.getEnterpriseTimeRule(organizationInfo);
    }

}
